<template>
  <div class="app-container calendar-list-container">
    <basic-container>
      <div class="filter-container">
        <el-button-group>
          <el-button
            v-if="deptManager_btn_add"
            type="primary"
            icon="plus"
            @click="handlerAdd">添加
          </el-button>
          <el-button
            v-if="deptManager_btn_edit"
            type="primary"
            icon="edit"
            @click="handlerEdit">编辑
          </el-button>
          <el-button
            v-if="deptManager_btn_del"
            type="primary"
            icon="delete"
            @click="handleDelete">删除
          </el-button>
        </el-button-group>
      </div>

      <el-row>
        <el-col
          :span="8"
          style="margin-top:15px;">
          <el-tree
            :data="treeData"
            :props="defaultProps"
            :filter-node-method="filterNode"
            class="filter-tree"
            node-key="id"
            highlight-current
            default-expand-all
            @node-click="getNodeData"/>
        </el-col>
        <el-col
          :span="16"
          style="margin-top:15px;">
          <el-card class="box-card">
            <el-form
              ref="form"
              :label-position="labelPosition"
              :rules="rules"
              :model="form"
              label-width="80px">
              <el-form-item
                label="父级节点"
                prop="parentName">
                <el-input
                  v-model="form.parentName"
                  disabled
                  placeholder="父级节点"/>
              </el-form-item>
              <el-form-item
                label="部门名称"
                prop="name">
                <el-input
                  v-model="form.name"
                  :disabled="formEdit"
                  placeholder="请输入名称"/>
              </el-form-item>
              <el-form-item label="部门编码"
                            prop="deptCode">
                <el-input v-model="form.deptCode"
                          :disabled="formEdit"
                          placeholder="请输入编码"></el-input>
              </el-form-item>
              <el-form-item
                label="排序"
                prop="sort">
                <el-input-number
                  class="number-input-width"
                  v-model="form.sort"
                  :disabled="formEdit"
                  :min="0"
                  :max="999"
                  controls-position="right"
                  :step="1"
                  step-strictly/>
              </el-form-item>
              <el-form-item label="简介"
                            prop="remark">
                <el-input type="textarea" v-model="form.remark"
                          :disabled="formEdit"
                          maxlength="200"
                          show-word-limit
                          placeholder="请输入简介（不得超过200字）"></el-input>
              </el-form-item>
              <el-form-item v-if="formStatus == 'update'">
                <el-button
                  type="primary"
                  @click="update">更新
                </el-button>
                <el-button @click="onCancel">取消</el-button>
              </el-form-item>
              <el-form-item v-if="formStatus == 'create'">
                <el-button
                  type="primary"
                  @click="create">保存
                </el-button>
                <el-button @click="onCancel">取消</el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
      </el-row>
    </basic-container>
  </div>
</template>

<script>
import { addObj, delObj, fetchTree, getObj, putObj } from '@/api/admin/dept'
import { mapGetters } from 'vuex'
import { validateStr } from '@/util/validate'

export default {
  name: 'Dept',
  data() {
    const validateDept = (rule, value, callback) => {
      if (validateStr(value)[0]) {
        callback(new Error(validateStr(value)[1]))
      } else {
        callback()
      }
    }
    const validateSort = (rule, value, callback) => {
      if (value > 100) {
        callback(new Error('排序不得大于100'))
      } else {
        callback()
      }
    }
    return {
      list: null,
      total: null,
      formEdit: true,
      formAdd: true,
      formStatus: '',
      typeOptions: ['0', '1'],
      methodOptions: ['GET', 'POST', 'PUT', 'DELETE'],
      listQuery: {
        name: undefined
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur'}
        ],
        deptCode: [
          {required: true, message: '请输入部门编码', trigger: 'blur'},
          {min: 2,max: 20, message: '长度在2 到 20 个字符', trigger: 'blur'},
          {validator: validateDept, trigger: 'blur'}
        ],
        sort: [
          { required: true, message: '请输入排序值', trigger: 'blur' },
          // {validator: validateSort, trigger: 'blur'}
        ],

      },
      labelPosition: 'right',
      form: {
        name: undefined,
        deptCode: undefined,
        sort: 999,
        remark: undefined,
        parentId: undefined,
        parentName: undefined
      },
      currentId: 0,
      currentName: "",
      currentHasChild: false,
      deptManager_btn_add: false,
      deptManager_btn_edit: false,
      deptManager_btn_del: false
    }
  },
  created() {
    this.getList()
    this.deptManager_btn_add = this.permissions['sys_dept_add']
    this.deptManager_btn_edit = this.permissions['sys_dept_edit']
    this.deptManager_btn_del = this.permissions['sys_dept_del']
  },
  computed: {
    ...mapGetters([
      'elements',
      'permissions'
    ])
  },
  methods: {
    getList() {
      fetchTree(this.listQuery).then(response => {
        this.treeData = response.data.data
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getNodeData(data) {
      if(data.children.length>0){
    		this.currentHasChild = true;
      } else {
    		this.currentHasChild = false;
      }
      if (!this.formEdit) {
        this.formStatus = 'update'
      }
      getObj(data.id).then(response => {
        this.form = response.data.data
      })
      this.currentId = data.id
      this.currentName = data.name
    },
    handlerEdit() {
      if (this.form.deptId) {
        this.formEdit = false
        this.formStatus = 'update'
      }
    },
    handlerAdd() {
      this.resetForm()
      this.formEdit = false
      this.formStatus = 'create'
    },
    handleDelete() {
      if(this.currentId == 0){
    		this.$alert('请选择记录进行删除');
    		return false;
    	}
    	if(this.currentHasChild){
    		this.$alert('该部门已有子部门，不能删除');
    		return false;
    	}
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delObj(this.currentId).then(() => {
          this.getList()
          this.resetForm()
          this.onCancel()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    update() {
      this.$refs.form.validate((valid) => {
        if (!valid) return
        putObj(this.form).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    create() {
      this.$refs.form.validate((valid) => {
        if (!valid) return
        addObj(this.form).then(() => {
          this.getList()
          this.resetForm()
          this.onCancel()
          this.$notify({
            title: '成功',
            message: '创建成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    onCancel() {
      this.formEdit = true
      this.formStatus = ''
      this.currentId = 0
      this.currentName = ""
      this.currentHasChild = false
      this.$refs["form"].resetFields()
      this.resetForm()
    },
    resetForm() {
      this.form = {
          name: undefined,
          deptId: undefined,
          deptCode: undefined,
          parentId: this.currentId,
          parentName: this.currentName,
          orderNum: undefined,
          remark: undefined
      }
    }
  }
}
</script>
